#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    initUI();
    initSignalSlots();
    setWindowTitle("Form created manually");
}

Dialog::~Dialog()
{
}

void Dialog::initUI()
{
    checkBoxUnder = new QCheckBox(tr("Underline"));
    checkBoxItalic = new QCheckBox(tr("Italic"));
    checkBoxBold = new QCheckBox(tr("Bold"));
    QHBoxLayout *hLay1 = new QHBoxLayout;
    hLay1->addWidget(checkBoxUnder);
    hLay1->addWidget(checkBoxItalic);
    hLay1->addWidget(checkBoxBold);
    //radio button
    rBtnBlack = new QRadioButton(tr("Black"));
    rBtnBlue = new QRadioButton(tr("Blue"));
    rBtnRed = new QRadioButton(tr("Red"));
    QHBoxLayout *hLay2 = new QHBoxLayout;
    hLay2->addWidget(rBtnBlack);
    hLay2->addWidget(rBtnBlue);
    hLay2->addWidget(rBtnRed);
    //pushbutton
    btnOk = new QPushButton(tr("确定"));
    btnCancel = new QPushButton(tr("取消"));
    btnClose = new QPushButton(tr("退出"));
    QHBoxLayout *hLay3 = new QHBoxLayout;
    hLay3->addStretch();
    hLay3->addWidget(btnOk);
    hLay3->addWidget(btnCancel);
    hLay3->addStretch();
    hLay3->addWidget(btnClose);
    //text
    textEdit = new QPlainTextEdit(tr("Hello World\nIt is a demo"));
    QFont font = textEdit->font();
    font.setPointSize(20);
    textEdit->setFont(font);
    //layout
    QVBoxLayout *vLay1 = new QVBoxLayout;
    vLay1->addLayout(hLay1);
    vLay1->addLayout(hLay2);
    vLay1->addWidget(textEdit);
    vLay1->addLayout(hLay3);

    setLayout(vLay1);
}

void Dialog::initSignalSlots()
{
    //checkBox
    connect(checkBoxUnder, SIGNAL(clicked(bool)), this, SLOT(onCheckBoxUnder(bool)));
    connect(checkBoxItalic, SIGNAL(clicked(bool)), this, SLOT(onCheckBoxItalic(bool)));
    connect(checkBoxBold, SIGNAL(clicked(bool)), this, SLOT(onCheckBoxBold(bool)));
    //radio button
    connect(rBtnBlack, SIGNAL(clicked()), this, SLOT(setTextColor()));
    connect(rBtnBlue, SIGNAL(clicked()), this, SLOT(setTextColor()));
    connect(rBtnRed, SIGNAL(clicked()), this, SLOT(setTextColor()));
    //PushButton
    connect(btnOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
    connect(btnClose, SIGNAL(clicked()), this, SLOT(close()));
}

void Dialog::onCheckBoxUnder(bool checked)
{
    QFont font = textEdit->font();
    font.setUnderline(checked);
    textEdit->setFont(font);
}

void Dialog::onCheckBoxItalic(bool checked)
{
    QFont font = textEdit->font();
    font.setItalic(checked);
    textEdit->setFont(font);
}

void Dialog::onCheckBoxBold(bool checked)
{
    QFont font = textEdit->font();
    font.setBold(checked);
    textEdit->setFont(font);
}

void Dialog::setTextColor()
{
    QPalette palette = textEdit->palette();
    if (rBtnBlack->isChecked())
        palette.setColor(QPalette::Text, Qt::black);
    else if (rBtnBlue->isChecked())
        palette.setColor(QPalette::Text, Qt::blue);
    else if (rBtnRed->isChecked())
        palette.setColor(QPalette::Text, Qt::red);
    else
        palette.setColor(QPalette::Text, Qt::black);
    textEdit->setPalette(palette);
}

